import datetime
import os.path
import random
import secrets
import time
import logging
import pprint
import requests
from globally import LogFile, product_id, CVECSVFILE, INFLUENCE_INFO, CVEJSONFILE
import csv, json
from functools import cached_property






class KylinKYSA:
    _KYSA_INTERFACES_URL = "https://support.kylinos.cn/protalweb/security/sa/info"

    def __init__(self, kysa_number):
        self.kysa_number = kysa_number
        self.response_obj = self._get_kysa_response()

    def _get_kysa_response(self):
        # 获取cve网页返回信息，POST请求
        UserAgent_List = [
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
            "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.6613.84 Safari/537.36 RuxitSynthetic/1.0 v100801619688 t564353316429258614 athccb4ad0b altpub cvcv=2 smf=0",
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 GLS/100.10.9989.100",
            "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4494.0 Safari/537.36",
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.4.154.18 Safari/525.19",
            "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.462.0 Safari/534.3"]
        headers = {
            "User-Agent": secrets.choice(UserAgent_List),
            "content-type": "application/json",
            "Cookie": "_qimei_uuid42=195120a1d03100a42e6bdbc9f3901730e3968b772c; _qimei_i_3=41fc7d87900853de97c4aa375b8d70b1f7eff7f0415f0384b78a7b5c70962839636762943989e2828996; _qimei_h38=; Hm_lvt_d4b0a47c0ecab35f0ad982a34e98471d=1747964986,1748226329,1748916311,1749109606; HMACCOUNT=29A0D7A828A86A9A; Hm_lvt_10cf3d4aa278c0a4e61c82a72c64b04e=1748420068,1748929338,1749024876,1749174603; HMACCOUNT=29A0D7A828A86A9A; _qimei_fingerprint=85b63b88837d47436469b64c34f6f3ec; _qimei_i_1=77c64af9961f; Hm_lpvt_10cf3d4aa278c0a4e61c82a72c64b04e=1749178343; Hm_lpvt_d4b0a47c0ecab35f0ad982a34e98471d=1749178499"
            }
        data = {"saNo": self.kysa_number}
        response = requests.post(url=KylinKYSA._KYSA_INTERFACES_URL, data=json.dumps(data), headers=headers, verify=False).json()
        # pprint.pprint(response)
        return response["obj"]

    @property
    def kysa_report(self) -> list:
        kysa_report = self._packages()
        return kysa_report

    def _packages(self):
        kysa_datas = []
        for srpm in self.response_obj["packages"].keys():
            for _productid in self.response_obj["packages"][srpm].keys():
                for _framework in self.response_obj["packages"][srpm][_productid].keys():
                    kysa_data = {"kysa_number": self._kysa_number,
                                 "title": self._kysa_title,
                                 "productid": _productid,
                                 "framework": _framework,
                                 "packages": self.response_obj["packages"][srpm][_productid][_framework]}
                    kysa_datas.append(kysa_data)
        return kysa_datas

    @property
    # KYSA公告的编号 举例：KYSA-202409-1029
    def _kysa_number(self):
        return self.response_obj["information"]["sa_no"]

    @property
    # KYSA公告的标题（title）
    def _kysa_title(self):
        return self.response_obj["information"]["title"]


